﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.IO;
using ConstellationShow.Function.DocData;
using System.Windows.Media.Imaging;

namespace ConstellationShow.Pages
{
    public partial class Luck : MyPage
    {
        bool[] isLoaded = { false, false, false, false, false };  //已加载数据的轴
        bool isInitLoad = false; //初始加载完成
        string id = "";  //星座ID
        string url = ""; //初始URL  不带Index的
        public Luck()
        {
            InitializeComponent();
            piv.SelectionChanged += new SelectionChangedEventHandler(piv_SelectionChanged);
            Loaded += (_, __) =>
            {
                int InitIndex = Convert.ToInt32(NavigationContext.QueryString["index"]);
                id = NavigationContext.QueryString["id"];
                url = Mark.MarkUrl(Url.GetStar);
                _progress.IsVisible = true;
                piv.SelectedIndex = InitIndex;
                img_txt1.Text = img_txt11.Text = img_txt21.Text = img_txt31.Text = Info.ConvStr(id);
                img_txt2.Text = img_txt12.Text = img_txt22.Text = img_txt32.Text = Com.ConsMonth(Convert.ToInt32(id));
                img.Source = img1.Source = img2.Source = img3.Source =
                    new BitmapImage(new Uri("/Images/" + Com.ConsImg(Convert.ToInt32(id)), UriKind.Relative));
                GetData(url + "&consnum=" + id + "&type=" + InitIndex, InitIndex);
            };
        }

        void piv_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = piv.SelectedIndex;
            if (!isInitLoad || isLoaded[index]) return;
            _progress.IsVisible = true;
            GetData(url + "&consnum=" + id + "&type=" + index, index);
        }

        //拉取信息
        void GetData(string _url, int loadIndex)
        {
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += (_, __) =>
            {
                if (__.Error != null)
                {
                    MessageBox.Show("网络错误，请检查网络是否连接好！");
                    _progress.IsVisible = false; return;
                };
                using (Stream streamResult = __.Result)
                {
                    XElement root = XElement.Load(streamResult);
                    var tests = from el in root.Elements("resultinfo") select el;
                    foreach (var a in tests)
                    {
                        if (a.Attribute("result").Value == "1") //成功
                        {
                            List<string> _shortremark = new List<string>();
                            List<string> _remainmark = new List<string>();
                            string _allast = ""; string _entast = "";
                            string _lovast = ""; string _estast = "";
                            string _data = ""; string _nongli = "";

                            var allast = from el in root.Elements("allast") select el;
                            var entast = from el in root.Elements("entast") select el;
                            var lovast = from el in root.Elements("lovast") select el;
                            var estast = from el in root.Elements("estast") select el;
                            var data = from el in root.Elements("date") select el;
                            var nongli = from el in root.Elements("nongli") select el;
                            var shortremark = from el in root.Elements("shortremark") select el;
                            var remainmark = from el in root.Elements("remainmark") select el;
                            foreach (var b in allast)
                                _allast = b.Value;
                            foreach (var b in entast)
                                _entast = b.Value;
                            foreach (var b in lovast)
                                _lovast = b.Value;
                            foreach (var b in estast)
                                _estast = b.Value;
                            foreach (var b in data)
                                _data = b.Value;
                            foreach (var b in nongli)
                                _nongli = b.Value;
                            foreach (var b in shortremark)
                                _shortremark = Com.ParseLuck(b.Value, loadIndex);
                            foreach (var b in remainmark)
                                _remainmark = Com.ParseLuck2(b.Value); ;
                            if (loadIndex == 0)
                            {
                                s1.mark = Convert.ToInt32(_allast);
                                s2.mark = Convert.ToInt32(_entast);
                                s3.mark = Convert.ToInt32(_lovast);
                                s4.mark = Convert.ToInt32(_estast);
                                txt1.Text = _shortremark[0].Trim();
                                txt2.Text = _shortremark[1].Trim();
                                txt3.Text = _shortremark[2].Trim();
                                txt4.Text = _shortremark[3].Trim() + "\n" + _shortremark[4].Trim();
                                txt5.Text = _shortremark[5].Trim();
                                txt6.Text = _data;
                                txt7.Text = _nongli;
                                txt8.Text = _remainmark[0].Trim();
                                txt9.Text = _remainmark[1].Trim();
                                txt10.Text = _remainmark[2].Trim();
                                txt11.Text = _remainmark[3].Trim();
                            }
                            else if (loadIndex == 1)
                            {
                                s11.mark = Convert.ToInt32(_allast);
                                s12.mark = Convert.ToInt32(_entast);
                                s13.mark = Convert.ToInt32(_lovast);
                                s14.mark = Convert.ToInt32(_estast);
                                txt14.Text = _shortremark[0].Trim();
                                txt15.Text = _shortremark[1].Trim();
                                txt16.Text = _shortremark[2].Trim();
                                txt17.Text = _shortremark[3].Trim() + "\n" + _shortremark[4].Trim();
                                txt18.Text = _shortremark[5].Trim();
                                txt12.Text = _data;
                                txt13.Text = _nongli;
                                txt19.Text = _remainmark[0].Trim();
                                txt20.Text = _remainmark[1].Trim();
                                txt21.Text = _remainmark[2].Trim();
                                txt22.Text = _remainmark[3].Trim();
                            }
                            else if (loadIndex == 2)
                            {
                                s21.mark = Convert.ToInt32(_allast);
                                s22.mark = Convert.ToInt32(_entast);
                                s23.mark = Convert.ToInt32(_lovast);
                                s24.mark = Convert.ToInt32(_estast);
                                txt25.Text = _shortremark[0].Trim();
                                txt26.Text = _shortremark[1].Trim();
                                txt27.Text = _shortremark[2].Trim();
                                txt28.Text = _shortremark[3].Trim();
                                txt29.Text = _shortremark[4].Trim();
                                txt56.Text = _shortremark[5].Trim();
                                txt57.Text = _shortremark[6].Trim();
                                txt23.Text = _data;
                                txt30.Text = _remainmark[0].Trim();
                                txt31.Text = _remainmark[1].Trim();
                                txt32.Text = _remainmark[2].Trim();
                                txt33.Text = _remainmark[3].Trim();
                            }
                            else if (loadIndex == 3)
                            {
                                s31.mark = Convert.ToInt32(_allast);
                                s32.mark = Convert.ToInt32(_entast);
                                s33.mark = Convert.ToInt32(_lovast);
                                s34.mark = Convert.ToInt32(_estast);
                                txt36.Text = _shortremark[0].Trim();
                                txt37.Text = _shortremark[1].Trim();
                                txt38.Text = _shortremark[2].Trim();
                                txt39.Text = _shortremark[3].Trim();
                                txt40.Text = _shortremark[4].Trim();
                                txt34.Text = _data;
                                txt41.Text = _remainmark[0].Trim();
                                txt42.Text = _remainmark[1].Trim();
                                txt43.Text = _remainmark[2].Trim();
                                txt44.Text = _remainmark[3].Trim();
                            }
                            _progress.IsVisible = false;
                            isLoaded[loadIndex] = true;
                            if (!isInitLoad) isInitLoad = true;
                        }
                    }
                }
            };
            webClient.OpenReadAsync(new Uri(_url), webClient);
        }

        private void Click_Share(object sender, EventArgs e)
        {
            string texts = "";
            if (piv.SelectedIndex == 0) texts = txt8.Text;
            else if (piv.SelectedIndex == 1) texts = txt19.Text;
            else if (piv.SelectedIndex == 2) texts = txt30.Text;
            else if (piv.SelectedIndex == 3) texts = txt41.Text;
            Com.Share(Info.ConvStr(id.ToString()) + "运程", texts);
        }
    }
}